package luj.game.robot.internal.concurrent.parent.command;

import luj.game.robot.internal.concurrent.parent.RobotBossDependency;
import luj.game.robot.internal.concurrent.parent.RobotParentActor;
import luj.game.robot.internal.instance.boss.command.execute.invoke.BossExecInvoker;
import luj.game.robot.internal.instance.boss.command.map.BossCommandMap;
import luj.spring.anno.Internal;

import static com.google.common.base.Preconditions.checkNotNull;

@Internal
final class OnExecCommand implements RobotParentActor.Handler<BossExecCommandMsg> {

  @Override
  public void onHandle(Context ctx) {
    RobotParentActor self = ctx.getActorState(this);
    BossExecCommandMsg msg = ctx.getMessage(this);

    RobotBossDependency dep = self.getBossDependency();
    BossCommandMap cmdMap = dep.getCommandMap();

    String cmdType = msg.commandType();
    BossCommandMap.Cmd cmd = cmdMap.get(cmdType);
    checkNotNull(cmd, cmdType);

    new BossExecInvoker(cmd, msg.param(), self).invoke();
  }
}
